package biz.obake.team.touchprotector.tpas;

import android.accessibilityservice.AccessibilityService;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;
import biz.obake.team.touchprotector.b;
import biz.obake.team.touchprotector.c;
import java.util.Map;

public class TPAccessibilityService extends AccessibilityService {

    /* renamed from: b  reason: collision with root package name */
    private Handler f1782b = new Handler();
    /* access modifiers changed from: private */

    /* renamed from: c  reason: collision with root package name */
    public KeyEvent f1783c;
    /* access modifiers changed from: private */

    /* renamed from: d  reason: collision with root package name */
    public biz.obake.team.touchprotector.b f1784d;
    /* access modifiers changed from: private */
    public boolean e = false;
    private ServiceConnection f = new b();

    class a implements Runnable {
        a() {
        }

        public void run() {
            if (TPAccessibilityService.this.g()) {
                try {
                    TPAccessibilityService.this.f1784d.d0(TPAccessibilityService.this.f1783c.getAction(), TPAccessibilityService.this.f1783c.getKeyCode(), TPAccessibilityService.this.f1783c.getRepeatCount(), true, SystemClock.uptimeMillis());
                } catch (RemoteException unused) {
                }
            }
        }
    }

    class b implements ServiceConnection {
        b() {
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            biz.obake.team.touchprotector.b unused = TPAccessibilityService.this.f1784d = b.a.x0(iBinder);
            boolean unused2 = TPAccessibilityService.this.e = false;
            try {
                TPAccessibilityService.this.f1784d.c0("TPAS.State", "on");
            } catch (RemoteException unused3) {
            }
        }

        public void onServiceDisconnected(ComponentName componentName) {
            biz.obake.team.touchprotector.b unused = TPAccessibilityService.this.f1784d = null;
            boolean unused2 = TPAccessibilityService.this.e = false;
        }
    }

    private void f(String str, String str2, AccessibilityEvent accessibilityEvent) {
        if (Build.VERSION.SDK_INT < 26 || !"Locked".equals(str2)) {
            return;
        }
        if ("com.android.systemui".equals(str) || "android".equals(str)) {
            int eventType = accessibilityEvent.getEventType();
            if (eventType == 32 || eventType == 2048) {
                this.f1784d.c0("NotificationDrawerCloser.Trigger", "tpas");
            }
        }
    }

    /* access modifiers changed from: private */
    public boolean g() {
        if (this.f1784d != null) {
            return true;
        }
        k();
        return false;
    }

    public static void h(Map<String, String> map) {
        map.put("TPAS.FrontApp", (Object) null);
        map.put("TPAS.State", "off");
    }

    public static boolean i() {
        return c.o("biz.obake.team.touchprotector");
    }

    private void j(String str, String str2) {
        if ((!"biz.obake.team.touchprotector".equals(str) || !"Locked".equals(str2)) && !"com.android.systemui".equals(str)) {
            this.f1784d.c0("TPAS.FrontApp", str);
        }
    }

    private void k() {
        if (this.f1784d == null && !this.e) {
            this.e = bindService(new Intent(biz.obake.team.android.a.a(), TPBridgeService.class), this.f, 1);
        }
    }

    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        CharSequence packageName;
        if (g()) {
            try {
                String Q = this.f1784d.Q();
                if (Q != null && !"Stopped".equals(Q) && (packageName = accessibilityEvent.getPackageName()) != null) {
                    String charSequence = packageName.toString();
                    j(charSequence, Q);
                    f(charSequence, Q, accessibilityEvent);
                }
            } catch (RemoteException unused) {
            }
        }
    }

    public void onCreate() {
        super.onCreate();
        k();
    }

    public void onDestroy() {
        biz.obake.team.touchprotector.b bVar = this.f1784d;
        if (bVar != null) {
            try {
                bVar.X();
                this.f1784d.c0("TPAS.State", "on");
            } catch (RemoteException unused) {
            }
            unbindService(this.f);
        }
        super.onDestroy();
    }

    public void onInterrupt() {
    }

    /* access modifiers changed from: protected */
    public boolean onKeyEvent(KeyEvent keyEvent) {
        if (keyEvent.getAction() == 0) {
            this.f1783c = keyEvent;
            this.f1782b.removeCallbacksAndMessages((Object) null);
            this.f1782b.postDelayed(new a(), 500);
        } else if (1 == keyEvent.getAction()) {
            this.f1782b.removeCallbacksAndMessages((Object) null);
        }
        boolean z = false;
        if (g()) {
            try {
                z = this.f1784d.d0(keyEvent.getAction(), keyEvent.getKeyCode(), keyEvent.getRepeatCount(), false, keyEvent.getEventTime());
            } catch (RemoteException unused) {
            }
        }
        return !z ? super.onKeyEvent(keyEvent) : z;
    }
}
